package com.virtual.rerijaapps.pet.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.BaseAdapter;

import com.androidquery.AQuery;
import com.virtual.rerijaapps.pet.PrincipalScreen;
import com.virtual.rerijaapps.pet.R;
import com.virtual.rerijaapps.pet.animation.FlipAnimation;
import com.virtual.rerijaapps.pet.data.Event;
import com.virtual.rerijaapps.pet.data.PetData;
import com.virtual.rerijaapps.pet.sound.SoundPlayer;
import com.virtual.rerijaapps.pet.ui.NewEventInclude;

public class PetBoxAdapter extends BaseAdapter implements OnClickListener
{

	private Context mContext;
	
	private ArrayList<PetData> mPetData;
	
	public PetBoxAdapter( Context context, ArrayList<PetData> petData )
	{
		this.mContext = context;
		this.mPetData = petData;
	}
	
	@Override
	public int getCount() 
	{
		return null != mPetData ? mPetData.size() : 0;
	}
	
	public void removePet( PetData dataToRemove )
	{
		mPetData.remove( dataToRemove );
	}
	
	public void removePet( int dataToRemove )
	{
		com.virtual.rerijaapps.pet.data.dao.PetDataDao petDataDao = new com.virtual.rerijaapps.pet.data.dao.PetDataDao( mContext );
		petDataDao.delete( mPetData.get( dataToRemove ).getId() );
		Log.i( "PETDATA_DELETE", "PetData eliminado, ID: " + mPetData.get( dataToRemove ).getId() );
		mPetData.remove( dataToRemove );
	}
	
	public void insertPet( PetData dataToInsert, int to )
	{
		mPetData.add( to, dataToInsert );
	}

	@Override
	public Object getItem( int position ) 
	{
		return null != mPetData ? mPetData.get( position ) : null;
	}

	@Override
	public long getItemId( int position ) 
	{
		return 0;
	}

	@Override
	public View getView( int position, View view, ViewGroup group ) 
	{
		
		View viewRoot = view;
		
		if ( null == view )
		{
			LayoutInflater inflater = ( LayoutInflater ) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
			view = viewRoot = inflater.inflate( R.layout.pet_box, null );			
		}
		
		final View fakeRoot = viewRoot;
		
		final AQuery aquery = new AQuery( view );
		
		if ( null != aquery )
		{
			
			aquery.id( R.id.pet_name ).text( mPetData.get( position ).getName() );
			aquery.id( R.id.pet_color ).backgroundColor( mPetData.get( position ).getColor() );
			aquery.id( R.id.pet_single_line ).backgroundColor( mPetData.get( position ).getColor() );
			aquery.id( R.id.pet_info_name ).text( mContext.getString( R.string.info_name ) + " " + mPetData.get( position ).getName() );
			aquery.id( R.id.pet_info_sex ).text( mContext.getString( R.string.info_sex ) + " " + ( mPetData.get( position ).isSex() ? mContext.getString( R.string.add_pet_sex_female ) : mContext.getString( R.string.add_pet_sex_male ) ) );
			aquery.id( R.id.pet_info_year ).text( mContext.getString( R.string.info_year ) + " " + String.valueOf( mPetData.get( position ).getAge() ) );
			aquery.id( R.id.pet_type ).image( mPetData.get( position ).isDog() ? R.drawable.cat_icon : R.drawable.dog_icon );
			
			mPetData.get( position ).setList_position( position );
			
			com.virtual.rerijaapps.pet.data.dao.PetDataDao petDataDao = new com.virtual.rerijaapps.pet.data.dao.PetDataDao( mContext );
			petDataDao.update( mPetData.get( position ) );
			
			com.virtual.rerijaapps.pet.data.dao.EventDao eventDao = new com.virtual.rerijaapps.pet.data.dao.EventDao( mContext );
			Event exampleEvent = new Event();
			exampleEvent.setPetdatafk( mPetData.get( position ).getId() );
			
			List<Event> listEvents = eventDao.listByExample( exampleEvent );
			
			if ( null == listEvents ||  0 == listEvents.size() )
			{
				aquery.id( R.id.pet_info_num_events ).text( mContext.getString( R.string.info_num_events ) +  " 0" );
			}
			else
			{
				aquery.id( R.id.pet_info_num_events ).text( mContext.getString( R.string.info_num_events ) + " " + String.valueOf( listEvents.size() ) );
			}
			
			final View front = aquery.id( R.id.pet_front ).getView();
			final View back = aquery.id( R.id.pet_back ).getView();
			
			aquery.id( R.id.pet_details ).gone();
			
			if ( View.GONE == front.getVisibility() )
			{
				FlipAnimation flipAnimation = new FlipAnimation( front, back, 0, null, 0 );
				flipAnimation.reverse();
				view.startAnimation( flipAnimation );
			}
			
			OnClickListener OnClickViewMore = new OnClickListener() 
			{
				@Override
				public void onClick( View view ) 
				{
					SoundPlayer.playSound( mContext, R.raw.pop );
				}
			};
			
			aquery.id( R.id.pet_view_more ).clicked( OnClickViewMore );
			
			OnClickListener onClickAnimation = new OnClickListener() 
			{
				@Override
				public void onClick( View view ) 
				{
					
					boolean isFront = true;
					
					FlipAnimation flipAnimation = new FlipAnimation( front, back, 500, null, 0 );
					
					int sound = R.raw.flip1;
					
					if ( View.GONE == front.getVisibility() )
					{
						flipAnimation.reverse();
						sound = R.raw.flip2;
						isFront = false;
					}
					else
					{
						isFront = true;
					}
					
					aquery.id( R.id.pet_front ).tag( isFront );
					
					flipAnimation.setAnimationListener( new AnimationListener() 
					{
						@Override
						public void onAnimationStart( Animation animation ) 
						{
							if ( !Boolean.parseBoolean( String.valueOf( aquery.id( R.id.pet_front ).getTag() ) ) )
							{
								aquery.id( R.id.pet_details ).gone().animate( R.anim.fadeout_short );
							}
						}
						
						@Override
						public void onAnimationRepeat( Animation animation ) 
						{
						}
						
						@Override
						public void onAnimationEnd( Animation animation ) 
						{
							if ( Boolean.parseBoolean( String.valueOf( aquery.id( R.id.pet_front ).getTag() ) ) )
							{
								aquery.id( R.id.pet_details ).visible().animate( R.anim.fadein_short );
							}
						}
					} );
					SoundPlayer.playSound( mContext, sound );
					fakeRoot.startAnimation( flipAnimation );
				}
			};
			
			aquery.id( R.id.pet_flip_icon ).clicked( onClickAnimation );
			aquery.id( R.id.pet_flip_icon2 ).clicked( onClickAnimation );
			aquery.id( R.id.pet_view_more ).clicked( this ).tag( mPetData.get( position ) );
			
		}
		
		return viewRoot;
	}

	@Override
	public void onClick( View v ) 
	{
		if ( null != v.getTag() )
		{
			( ( PrincipalScreen ) mContext ).mAquery.id( ( ( PrincipalScreen ) mContext ).mNewEvent ).visible().animate( R.anim.scale_up_in );
			( ( NewEventInclude ) ( ( PrincipalScreen ) mContext ).mAquery.id( ( ( PrincipalScreen ) mContext ).mNewEvent ).getView() ).resetFields();
			( ( NewEventInclude ) ( ( PrincipalScreen ) mContext ).mAquery.id( ( ( PrincipalScreen ) mContext ).mNewEvent ).getView() ).setInfo( ( ( PetData ) v.getTag() ) );
			SoundPlayer.playSound( mContext, R.raw.pop );		}
	}

}
